<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wallet
 *
 * @author win7
 */
class invoicies extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/invoicies_model', 'invoicies');
        $this->load->model('admin/invoicie_items_model', 'invitems');
    }

    public function index($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->invoicies->filter(true);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->invoicies->filter(FALSE, $offset);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('invoicies/index', $this->pass);
    }

    public function confirminv($id = FALSE) {
        $this->invoicies->is_found(array('inv_num' => $id, 'payment_status' => 'NO', 'confirm_status' => 'NO'), array(), 'row', $this->agent->referrer());
        $update['confirm_status'] = 'YES';
        $update['confirm_date'] = date("Y-m-d H:i:s");
        $this->invoicies->save($update, array('inv_num' => $id), TRUE);
        $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_confirm_success'));
    }

    public function confirm_pay($id = FALSE) {
        $inv = $this->invoicies->is_found(array('inv_num' => $id, 'payment_status' => 'YES', 'confirm_status' => 'NO'), array(), 'row', $this->agent->referrer());
        if ($this->input->post()) {
            $update['confirm_status'] = 'YES';
            $update['confirm_date'] = date("Y-m-d H:i:s");
            if ($this->invoicies->save($update, array('inv_num' => $id), TRUE)) {
                if ($inv->pay_type == strtoupper('wallet')) {
                    $wallet['status'] = 'active';
                    $this->load->model('admin/wallet_model');
                    $this->wallet_model->save($wallet, array('id' => $inv->pay_id), TRUE);
                }
                if ($inv->for == 'create_store') {
                    $this->load->model('admin/stores_model');
                    $store['active'] = 'YES';
                    $store['start_date'] = date("Y-m-d H:i:s");
                    $store['end_date'] = date("Y-m-d H:i:s", time() + 31536000);
                    $this->stores_model->save($store, array('owner_id' => $inv->user_id), TRUE);
                } elseif ($inv->for == 'special_product') {
                    $this->load->model('admin/products_model', 'product');
                    $pro = $this->product->get(array('inv_num' => $id), false, 'row');
                    $product['special_product'] = 'YES';
                    $product['special_start'] = date('Y-m-d H:i:s');
                    $product['special_end'] = date('Y-m-d H:i:s', $pro->days * 24 * 3600 + time());
                    $this->product->save($product, array('inv_num' => $id), TRUE);
                } elseif ($inv->for == 'buyproduct') {
                    $this->load->model('admin/seller_items_model', 'sellite');
                    $selitem['type'] = 'SEND';
                    $selitem['seen'] = 'NO';
                    $this->sellite->save($selitem, array('inv_num' => $id, 'canceled' => 'NO'), TRUE);
                } elseif ($inv->for == 'upgrade_store') {
                    $this->load->model('admin/upgrade_store_model', 'ustore');
                    $this->load->model('admin/stores_model', 'stores');
                    $ustore = $this->ustore->is_found(array('invoice_id' => $id), array(), 'row', $this->agent->referrer());
                    $store['package_id'] = $ustore->package_id;
                    $store['package_values'] = $ustore->package_values;
                    $store['package_price'] = $ustore->package_price;
                    $store['invoice_id'] = $ustore->invoice_id;
                    $store['start_date'] = date("Y-m-d H:i:s");
                    $store['end_date'] = date("Y-m-d H:i:s", time() + 31536000);
                    $this->stores->save($store, array('owner_id' => $ustore->owner_id), TRUE);
                    $this->ustore->delete(array('invoice_id' => $id), FALSE);
                } elseif ($inv->for == 'advertise') {
                    $this->load->model('advertise_model');
                    $this->advertise_model->is_found(array('inv_num' => $id), array(), 'row', $this->agent->referrer());
                    $save['active'] = 'YES';
                    $this->advertise_model->save($save, array('inv_num' => $id), TRUE);
                }
                $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_confirm_success'));
            } else {
                get_save_error();
            }
        }
        $this->pass['inv'] = $inv;
        $this->themes->render('invoicies/confirmpay', $this->pass);
    }

    public function view($id = FALSE) {
        $falid_id = $this->invoicies->is_found(array('inv_num' => $id), array(), 'row', $this->agent->referrer());
        $data['result'] = $falid_id;
        if ($falid_id->itemes == 'YES' && $falid_id->cancel == 'NO') {
            $data['items'] = $items = $this->invitems->get(array('inv_num' => $falid_id->inv_num));
            $this->load->model('seller_items_model');
            $data['sdata'] = $this->seller_items_model->get(array('inv_num' => $falid_id->inv_num));
        }
        $this->themes->render('invoicies/view', $data);
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');
        switch ($doaction) {
            case 'rejected': {
                    $this->wallete->rejected($id);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete': {
                    $this->wallete->delete(array('id' => $id), FALSE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            default:
                redirect($this->agent->referrer());
                break;
        }
        //  echo json_encode($_POST['manager']);
    }

    public function delete($id = FALSE) {
        if ($id) {
            $falid_id = $this->wallete->is_found(array('wallet.id' => $id), array(), 'row', $this->agent->referrer());
            if ($this->wallete->delete(array('id' => $id), FALSE)) {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    function senditems($invnum = false) {
        if ($invnum) {
            $inv = $this->invoicies->is_found(array('inv_num' => $invnum, 'payment_status' => 'NO', 'confirm_status' => 'NO', 'itemes' => 'YES'), array(), 'row', $this->agent->referrer());
            $invitems = $this->invitems->get(array('inv_num' => $invnum));
            if (count($invitems) > 0) {
                $this->load->model('admin/seller_items_model', 'sellite');
                foreach ($invitems as $item) {
                    $temp = $this->sellite->get(array('item_id' => $item->id), false, 'row');
                    if (count($temp) > 0) {
                        $save['seen'] = 'NO';
                        $this->sellite->save($save, array('id' => $temp->id), TRUE);
                    } else {
                        $sellerid = get_store_id_from_product($item->product_id);
                        if ($invnum && $sellerid && $item->id) {
                            $save['inv_num'] = $invnum;
                            $save['seller_id'] = $sellerid;
                            $save['item_id'] = $item->id;
                            $this->sellite->save($save);
                        }
                    }
                }
            }
            $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_save_success'));
        } else {
            redirect(ADMIN_FOLDER);
        }
    }

    function cancel($id = false) {
        if ($id) {
            $inv = $this->invoicies->is_found(array('inv_num' => $id, 'payment_status' => 'NO'), array(), 'row', __CLASS__);
            $save['cancel'] = 'USER';
            if ($this->invoicies->save($save, array('inv_num' => $id), TRUE)) {
                switch ($inv->for) {
                    case 'create_store':
                        $this->load->model('admin/stores_model', 'stores');
                        $this->stores->delete(array('invoice_id' => $id), FALSE);
                        break;
                    case 'special_product':
                        break;
                    case 'buyproduct':
                        $this->invitems->delete(array('inv_num' => $id), FALSE);
                        $this->load->model('admin/seller_items_model', 'dealsmo');
                        $this->dealsmo->delete(array('inv_num' => $id), FALSE);
                        break;
                    case 'upgrade_store':
                        $this->load->model('upgrade_store_model');
                        $this->upgrade_store_model->delete(array('invoice_id' => $id), FALSE);
                        break;
                    case 'advertise':
                        $this->load->model('advertise_model');
                        $data = $this->advertise_model->get(array('inv_num' => $id));
                        if($data){
                            foreach ($data as $dt)
                                @unlink (ADS_PATH."/$dt->image");
                        }
                        $this->advertise_model->delete(array('inv_num' => $id), FALSE);
                        break;
                }
                $this->themes->set_message(array('url' => ADMIN_FOLDER . '/' . __CLASS__, 'type' => 'success', 'message' => 'message_cancel_success'));
            } else {
                $this->themes->set_message(array('url' => ADMIN_FOLDER . '/' . __CLASS__, 'type' => 'error', 'message' => 'message_cancel_error'));
            }
        }
        show_404();
    }

}

